package studio.raptor.ddal.dashboard.api;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import studio.raptor.ddal.core.log.bo.ConnPoolLogData;
import studio.raptor.ddal.core.log.bo.SqlProcessLogData;
import studio.raptor.ddal.dashboard.log.bo.ConnPoolLogExtendData;
import studio.raptor.ddal.dashboard.log.bo.SqlProcessLogExtendData;
import studio.raptor.ddal.dashboard.log.service.interfaces.ILogReceiveService;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;

/**
 * 类名称 ReceiveLogServlet <br>
 * 日志服务端接收Servlet
 * <hr>
 * 修改记录
 * <hr>
 * 1、修改人员:    修改时间:<br>
 *    修改内容:
 * <hr>
 */
@RestController
public class LogController extends HttpServlet {

	@Autowired
	public ILogReceiveService logReceiveService;


	@RequestMapping(value = "/log/sqlprocesslog", method = RequestMethod.POST)
	public void receiveLog(HttpServletRequest request, HttpServletResponse response)
			throws IOException {
		ObjectOutputStream outputStream = null;
		try{
//			Date acceptTime=new Date();
			Object logObj=receiveObject(request);
			if(logObj!=null){
				String appName=request.getParameter( "appName" );
				if(logObj instanceof SqlProcessLogData){
					SqlProcessLogData sqlProcessLogData=(SqlProcessLogData)logObj;
					SqlProcessLogExtendData sqlProcessLogExtendData=new SqlProcessLogExtendData();
					sqlProcessLogExtendData.clone( sqlProcessLogData );
//					sqlProcessLogExtendData.setAcceptTime( acceptTime );
					sqlProcessLogExtendData.setAppName( appName );
					logReceiveService.receiveObject(sqlProcessLogExtendData);
				}
				else if(logObj instanceof ConnPoolLogData){
					ConnPoolLogData connPoolLogData=(ConnPoolLogData)logObj;
					ConnPoolLogExtendData connPoolLogExtendData=new ConnPoolLogExtendData();
					connPoolLogExtendData.clone( connPoolLogData );
//					connPoolLogExtendData.setAcceptTime( acceptTime );
					connPoolLogExtendData.setAppName( appName );
					logReceiveService.receiveObject(connPoolLogExtendData);
				}
			}

			outputStream = new ObjectOutputStream(response.getOutputStream());
			outputStream.writeObject("操作成功");
			outputStream.flush();
			outputStream.close();
			outputStream = null;
		}catch(Exception e){
			e.printStackTrace();
		}finally {
			try{
				if(outputStream != null){
					outputStream.close();
					outputStream = null;
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}


	/**
	 * 接收客户日志端方法
	 * @param request
	 */
	public Object receiveObject(HttpServletRequest request){
		ObjectInputStream inputStream=null;
		try {
			inputStream = new ObjectInputStream(request.getInputStream());
//			SqlProcessLogData sqlProcessLogData = (SqlProcessLogData) inputStream.readObject();
			Object logObj=inputStream.readObject();
			inputStream.close();
			inputStream = null;
			return logObj;
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (inputStream != null) {
					inputStream.close();
					inputStream = null;
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return null;
	}

}
